package com.zjw.zy.coreui.fragment.holder

import android.graphics.Bitmap
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewHolder
import com.zjw.zy.bean.ImageInfo
import com.zjw.zy.utils.PathUtils
import com.zjw.zy.utils.ResourcesUtils

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/27 9:28
 * @description：
 */
class ImageContentHolder(item: View) : BaseViewHolder<ImageInfo>(item) {

    private val image: ImageView by lazy { findViewById(R.id.image_holder_image) }
    private val content: ViewGroup by lazy { findViewById(R.id.image_holder_content) }
    private var bitmap: Bitmap? = null

    private var isSelect = false

    override fun onBindListener() {
    }

    override fun onBindData(data: ImageInfo?) {
        data?.let {
            bitmap?.recycle()
            bitmap = PathUtils.getBitmapFromPath(data.path)
            image.setImageBitmap(bitmap)
        }
    }

    fun setSelect(s: Boolean) {
        isSelect = s
        if (isSelect) {
            content.background = ResourcesUtils.getDrawable(R.drawable.image_content_select_color)
        } else {
            content.background = null
        }
    }

    fun toggleSelect(): Boolean {
        if (isSelect) {
            content.background = null
            isSelect = false
        } else {
            content.background = ResourcesUtils.getDrawable(R.drawable.image_content_select_color)
            isSelect = true
        }

        return isSelect
    }

    fun markUsing(usingPath: String) {
        if (data.path == usingPath) {
            content.background = ResourcesUtils.getDrawable(R.drawable.image_content_using_color)
        } else {
            content.background = null
        }
    }
}